package com.babel.util.lide;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

/**
 * 对称加密工具类
 * @author lyj
 * @date 2017年5月17日---上午11:46:22
 */
public class DESedeUtil {

    /**
     * 密钥算法
     */
    public static final String KEY_ALGORITHM = "DESede";

    /**
     * 转换密钥
     * @param key 二进制密钥
     * @return KEY 密钥
     * @throws Exception
     */
    private static Key toKey(byte[] key) throws Exception {
	DESedeKeySpec dks = new DESedeKeySpec(key);
	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
	return keyFactory.generateSecret(dks);
    }

    /**
     * 解密
     * @param data  待解密数据
     * @param key   密钥
     * @return byte[]   解密数据
     * @throws Exception
     */
    public static byte[] decrypt(byte[] data, byte[] key, DESedePaddingType padding) throws Exception {
	Key k = toKey(key);
	Cipher cipher = Cipher.getInstance(padding.getCode());
	cipher.init(Cipher.DECRYPT_MODE, k);
	return cipher.doFinal(data);
    }

    /**
     * 加密
     * @param data  待加密数据
     * @param key   密钥
     * @return byte[]   加密数据
     * @throws Exception
     */
    public static byte[] encrypt(byte[] data, byte[] key, DESedePaddingType padding) throws Exception {
	Key k = toKey(key);
	Cipher cipher = Cipher.getInstance(padding.getCode());
	cipher.init(Cipher.ENCRYPT_MODE, k);
	return cipher.doFinal(data);
    }

    /**
     * 生成密钥  
     * @param keySize 密钥长度  3DES密钥长度可选值 112，168 分别对应ABA,ABC密钥模式
     * @return byte[]   二进制密钥
     * @throws Exception
     */
    public static byte[] initKey(DESedeKeySize keySize) throws Exception {
	KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
	kg.init(keySize.getSize());
	SecretKey secretKey = kg.generateKey();
	return secretKey.getEncoded();
    }

    /**
     * 生成密钥并base64转码
     * @return
     * @throws Exception
     */
    public static String initKeyStr(DESedeKeySize keySize) throws Exception {
	KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
	kg.init(keySize.getSize());
	SecretKey secretKey = kg.generateKey();
	return ByteUtil.base64Encode(secretKey.getEncoded());
    }

    public static void main(String args[]) {

	try {
	    // byte[] key = initKey(DESedeKeySize.ABC);

	    byte[] key = "+8d12stY5qsplIXVripJrbkI4BZRkVTl".getBytes();
	    String orgiStr = "6226621234567890";
	    byte[] endata = encrypt(orgiStr.getBytes(), key, DESedePaddingType.PKCS5Padding);

	    String cipherText = ByteUtil.byte2HexStr(endata);
	    System.out.println("cipherText=" + cipherText);

	    byte[] bank = ByteUtil.hex2bytes("98E3C463BC5E3856332DBAA00E0DB17C");
	    byte[] id = ByteUtil.hex2bytes("7C50C61AF6767DFBE20559C17540F9EC115DD20B53F6B018");

	    byte[] dedata1 = decrypt(bank, key, DESedePaddingType.PKCS5Padding);
	    byte[] dedata2 = decrypt(id, key, DESedePaddingType.PKCS5Padding);

	    System.out.println("bank:" + (new String(dedata1)));
	    System.out.println("id: " + new String(dedata2));
	    System.out.println(orgiStr.getBytes().length);
	    System.out.println(ByteUtil.byte2HexStr(orgiStr.getBytes()).length());

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}
